package cs340.stormtroopers.junitTests;

import java.util.Random;

import cs340.stormtroopers.hypeerweb.*;
import cs340.stormtroopers.phase2Tests.ExpectedResult;
import junit.framework.TestCase;

public class RandomTest extends TestCase
{
	public void test()
	{
		Random rand = new Random();
		HyPeerWeb h = HyPeerWeb.getSingleton();
		h.clear();
		Node n = new Node(0);
		h.addToHyPeerWeb(n, null);
		for (int i = 0; i < 100; i++)
		{
			System.out.print(i + ":\t");

			// random operation
			int r = rand.nextInt(3);
			if (h.size() > 1 && r == 2)
			{
				int remove = rand.nextInt(h.size());
				System.out.println("Removing " + remove);
				h.removeFromHyPeerWeb(h.getNode(remove));
			}
			else
			{
				System.out.println("Adding " + h.size());
				h.addToHyPeerWeb(new Node(0), null);
			}

			// test against expected
			for (int j = 0; j < h.size(); j++)
			{
				Node nodei = h.getNode(j);
				SimplifiedNodeDomain simplifiedNodeDomain = nodei
						.constructSimplifiedNodeDomain();
				ExpectedResult expectedResult = new ExpectedResult(h.size(), j);

				if(!simplifiedNodeDomain.equals(expectedResult))
				{
					System.out.println("\nGiven Information: \n" + simplifiedNodeDomain);
					System.out.println("\nExpected Information: \n" + expectedResult);

					fail();
				}

			}
		}
	}

	public void testAddRemoveAddRemove()
	{
		HyPeerWeb h = HyPeerWeb.getSingleton();
		h.clear();
		for (int i = 0; i < 6; i++)
			h.addToHyPeerWeb(new Node(i), null);
		Node node0 = h.getNode(0);
		for (int i = 0; i < 6; i++)
			h.removeFromHyPeerWeb(node0);
		for (int i = 0; i < 6; i++)
			h.addToHyPeerWeb(new Node(i), null);
		node0 = h.getNode(0);
		for (int i = 0; i < 6; i++)
			h.removeFromHyPeerWeb(node0);
	}
}
